using System.Collections.Generic;
using System.IO;
using DefaultNamespace;
using DefaultNamespace.Core;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.UI;

public class Main : MonoBehaviour
{
    [SerializeField] private Image m_Avatar;
    [SerializeField] private TextMeshProUGUI m_Text;
    [SerializeField] private Button m_Button;
    [SerializeField] private Image m_PendantImage;
    [SerializeField] private Image m_TileImage;
    
    
    void Start()
    {
        string platform = "Android";
        #if UNITY_ANDROID
            platform = "Android";
        #elif UNITY_IOS
            platform = "iOS";
        #endif
        
        Debug.Log($"Hello, {nameof(Main)}! - platform:{platform}");
        
        // RemoteAbProcess();

        var bundleList = JsonConvert.DeserializeObject<List<string>>(Resources.Load<TextAsset>("RemoteBundleList").text);
        Addressables.InitializeAsync();
        Addressables.InternalIdTransformFunc = location =>
        {
            if (location.Data is AssetBundleRequestOptions ab)
            {
                // Debug.Log($"--->>> internalId:{location.InternalId}, PrimaryKey:{location.PrimaryKey}");
                if ((location.InternalId.StartsWith("https://") || location.InternalId.StartsWith("http://")) && bundleList.Contains(location.PrimaryKey)) // 检测包体中是否包含该资源,优先使用包体中的资源
                {
                    var abPath = Path.Combine(Addressables.RuntimePath, platform, location.PrimaryKey);
                    // Debug.Log($"--->>> abPath:{abPath}");
                    return abPath;
                }   
            }
            return location.InternalId;
        };
        
        m_Button.onClick.AddListener(() =>
        {
            var index = Random.Range(1, 9);
            var handle = Addressables.LoadAssetAsync<Sprite>($"Assets/Res/Textures/Tile_{index}.png");
            handle.Completed += operationHandle =>
            {
                m_TileImage.sprite = operationHandle.Result;
            };
            
            var pIndex = Random.Range(2001, 2006);
            var pHandle = Addressables.LoadAssetAsync<Sprite>($"Assets/Res/Textures/Tile_{pIndex}.png");
            pHandle.Completed += operationHandle =>
            {
                m_PendantImage.sprite = operationHandle.Result;
            };
            
            Addressables.LoadAssetAsync<Sprite>("Assets/Res/Textures/Avatar00.png").Completed += packHandle =>
            {
                m_Avatar.sprite = packHandle.Result;
            };
        });
        
        Addressables.LoadAssetAsync<GameConfig>($"Assets/Res/GameConfig.asset").Completed += operationHandle =>
        {
            var config = operationHandle.Result;
            Addressables.LoadAssetAsync<BackpackAsset>(config.Backpack).Completed += packHandle =>
            {
                var backpack = packHandle.Result;
                m_Text.text = $"Version:{config.VersionName}\nDesc:{config.Desc}" 
                              + $"\nDName:{backpack.Props[0].Name}\nDDesc:{backpack.Props[0].Desc}";
            };
        };
    }

    private void RemoteAbProcess()
    {
        CoreCs.Enter();
    }
}
